a. Cookies
b. Session State
c. Application State.
a. Cookies :
Aspx.cs:
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
namespace WebApplication1
{
publicpartialclassPR4A : System.Web.UI.Page
{
protectedvoidPage_Load(object sender, EventArgs e)
{
}
protectedvoidWriteCookie(object sender, EventArgs e)
{
//Create a Cookie with a suitable Key.
HttpCookienameCookie = newHttpCookie("Name");
//Set the Cookie value.
nameCookie.Values["Name"] = txtName.Text;
//Set the Expiry date.
nameCookie.Expires = DateTime.Now.AddDays(30);
//Add the Cookie to Browser.
Response.Cookies.Add(nameCookie);
}
protectedvoidReadCookie(object sender, EventArgs e)
{
//Fetch the Cookie using its Key.
HttpCookienameCookie = Request.Cookies["Name"];
//If Cookie exists fetch its value.
string name = nameCookie != null ? nameCookie.Value.Split('=')[1] : "undefined";
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + name + "');", true);
}
protectedvoidRemoveCookie(object sender, EventArgs e)
{
//Fetch the Cookie using its Key.
HttpCookienameCookie = Request.Cookies["Name"];
//Set the Expiry date to past date.
nameCookie.Expires = DateTime.Now.AddDays(-1);
//Update the Cookie in Browser.
Response.Cookies.Add(nameCookie);
}
}
}
Aspx:
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="PR4A.aspx.cs"Inherits="WebApplication1.PR4A"%>
<!DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
<div>
Name:
<asp:TextBoxID="txtName"runat="server"/>
<br/>
<br/>
<asp:ButtonID="btnWrite"Text="Write Cookie"runat="server"OnClick="WriteCookie"/>
<asp:ButtonID="btnRead"Text="Read Cookie"runat="server"OnClick="ReadCookie"/>
<asp:ButtonID="btnDelete"Text="Remove Cookie"runat="server"OnClick="RemoveCookie"/>
</div>
</form>
</body>
</html>
OUTPUT:
b. Session State :
Aspx:
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="PR4B.aspx.cs"Inherits="WebApplication1.PR4B"%>
<!DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:LabelID="Label1"runat="server"Text="Label"></asp:Label>&NBSP;<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
<br/><br/>
<asp:ButtonID="Button1"runat="server"Text="Button"/>
</div>
</form>
</body>
</html>
Aspx.cs:
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
namespace WebApplication1
{
publicpartialclassPR4B : System.Web.UI.Page
{
protectedvoidPage_Load(object sender, EventArgs e)
{
}
protectedvoid Button1_Click(object sender, EventArgs e)
{
Session["username"] = TextBox1.Text;
Response.Redirect("PR4B1.aspx");
}
}
}
OUTPUT:
C. Application State:
Aspx:
<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="PR4C.aspx.cs"Inherits="WebApplication1.PR4C"%>
<!DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:ButtonID="Button1"runat="server"Text="Button"/>
</div>
</form>
</body>
</html>
Aspx.cs:
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
namespace WebApplication1
{
publicpartialclassPR4C : System.Web.UI.Page
{
protectedvoidPage_Load(object sender, EventArgs e)
{
}
protectedvoid Button1_Click(object sender, EventArgs e)
{
Response.Write("<h1>Your are user Number-->" + Application["hits"].ToString());
}
}
}
OUTPUT:
ASP.NET PROGRAMS
0 Comments